﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Web.Mvc;
using System.Web.Configuration;
using ServiceStack.Text;

namespace Plat.WebUtility
{
    public static class HttpClientType
    {
        public static readonly string CRUD = "CRUD";
        public static readonly string CRUD_CREATE = "C";
        public static readonly string CRUD_UPDATE = "U";
        public static readonly string CRUD_RETRIVE = "R";
        public static readonly string CRUD_DELETE = "D";
    }

    /// <summary>
    /// Mime内容格式
    /// </summary>
    public enum MimeFormat
    {
        XML = 0,
        JSON = 1
    }

    //public class HttpClientHelper
    //{
        
    //    public string GetString()
    //    {
    //        var response = HttpClient.GetAsync("").Result;
    //        var result = response.Content.ReadAsStringAsync().Result;
    //        return result;
    //    }

    //    public string Insert(string jsonValue)
    //    {
    //        StringContent content = new System.Net.Http.StringContent(jsonValue, Encoding.UTF8, "application/json");
    //        var response = HttpClient.PostAsync("", content).Result;
    //        var result = response.Content.ReadAsStringAsync().Result;
    //        return result;
    //    }

    //    public string Post(string jsonValue)
    //    {
    //        StringContent content = new StringContent(jsonValue, Encoding.UTF8, "application/json");
    //        var response = HttpClient.PostAsync("", content).Result;
    //        var result = response.Content.ReadAsStringAsync().Result;
    //        return result;
    //    }

    //    public string Update(string jsonValue)
    //    {
    //        StringContent content = new System.Net.Http.StringContent(jsonValue, Encoding.UTF8, "application/json");
    //        var response = HttpClient.PutAsync("", content).Result;
    //        var result = response.Content.ReadAsStringAsync().Result;
    //        return result;
    //    }

    //    public string Delete()
    //    {
    //        var response = HttpClient.DeleteAsync("").Result;
    //        var result = response.Content.ReadAsStringAsync().Result;
    //        return result;
    //    }
    //}

    public class HttpClientHelper
    {
        public string ApiRootUrl
        {
            get;
            set;
        }

        public string ActionUrl
        {
            get;
            set;
        }

        public HttpClient HttpClient
        {
            get;
            set;
        }

        protected HttpClientHelper(string apiRootUrl, string actionUrl)
        {
            ApiRootUrl = apiRootUrl;
            ActionUrl = actionUrl;
        }

        public HttpClient CreateClient()
        {
            string url = ApiRootUrl + ActionUrl;
            return Create(MimeFormat.JSON, url);
        }

        public HttpClient Create(MimeFormat format, string url)
        {
            HttpClient client = new HttpClient();
            switch (format)
            {
                case MimeFormat.XML:
                    client.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/xml"));
                    break;
                case MimeFormat.JSON:
                    client.DefaultRequestHeaders.Accept.Add(
                        new MediaTypeWithQualityHeaderValue("application/json"));
                    break;
            }

            if (url != string.Empty)
            {
                client.BaseAddress = new Uri(url);
            }
            return client;
        }

        public static HttpClientHelper Instance(string apiRootUrl, string actionUrl)
        {
            HttpClientHelper helper = new HttpClientHelper(apiRootUrl, actionUrl);
            HttpClient client = helper.CreateClient();
            helper.HttpClient = client;
            return helper;
        }

        public T2 Post<T1, T2>(T1 t) 
            where T1: class
            where T2: class
        {
            string jsonValue = JsonSerializer.SerializeToString<T1>(t);
            StringContent content = new StringContent(jsonValue, Encoding.UTF8, "application/json");
            var response = HttpClient.PostAsync("", content).Result;
            var message = response.Content.ReadAsStringAsync().Result;
            var result = JsonSerializer.DeserializeFromString<T2>(message);

            return result;
        }

        public T2 Get<T1, T2>(T1 t)
            where T1 : class
            where T2 : class
        {
            var response = HttpClient.GetAsync("").Result;
            var message = response.Content.ReadAsStringAsync().Result;
            var result = JsonSerializer.DeserializeFromString<T2>(message);

            return result;
        }

        public T2 Insert<T1, T2>(T1 t)
            where T1 : class
            where T2 : class
        {
            string jsonValue = JsonSerializer.SerializeToString<T1>(t);
            StringContent content = new System.Net.Http.StringContent(jsonValue, Encoding.UTF8, "application/json");
            var response = HttpClient.PostAsync("", content).Result;
            var message = response.Content.ReadAsStringAsync().Result;
            var result = JsonSerializer.DeserializeFromString<T2>(message);

            return result;
        }

        public T2 Update<T1, T2>(T1 t)
            where T1 : class
            where T2 : class
        {
            string jsonValue = JsonSerializer.SerializeToString<T1>(t);
            StringContent content = new System.Net.Http.StringContent(jsonValue, Encoding.UTF8, "application/json");
            var response = HttpClient.PutAsync("", content).Result;
            var message = response.Content.ReadAsStringAsync().Result;
            var result = JsonSerializer.DeserializeFromString<T2>(message);

            return result;
        }
    }
}
